
//应用详情
function loadappdetail(id) {
    fooddetailModel.clear();
    var xhr = new XMLHttpRequest();
    var url="http://api.9smart.cn/app/"+id
    console.log("URL:"+url);
    xhr.open("GET",url,true);
    xhr.onreadystatechange = function()
    {
        if ( xhr.readyState == xhr.DONE)
        {
            if ( xhr.status == 200)
            {
                var jsonObject = eval('(' + xhr.responseText + ')');

                loaded(jsonObject)
            }
        }
    }

    xhr.send();
}



function loaded(jsonObject)
{
    //    if(parseInt(jsonObject.pager[0].count)  == 0 ){
    //        header.title="无结果"
    //        root.visible=false
    //    }
    //    else{
    showappdetail.id=jsonObject.appid;
    showappdetail.downurl=jsonObject.url;
    header.title=jsonObject.appname;
    fooddetailModel.append({
                               "appname":jsonObject.appname,
                               "system": jsonObject.system,
                               "type": jsonObject.type,
                               "category": jsonObject.category,
                               "views": jsonObject.views,
                               "downloads":jsonObject.downloads,
                               "dateline": js_date_time(jsonObject.dateline),
                               "icon": jsonObject.icon,
                               "version": jsonObject.version,
                               "size": jsonObject.size,
                               "summary": jsonObject.summary,
                               "url": jsonObject.url,
                               "author": jsonObject.author,
                               "compatible":jsonObject.compatible,
                               "downloadname": jsonObject.downloadname
                           }
                           );
    for ( var i in jsonObject.screenshots   ){
        screenShotModel.append({
                                   "url":jsonObject.screenshots[i].url,
                                   "thumburl":jsonObject.screenshots[i].thumburl
                               });
    }



    progress.visible=false
}

function searchbyauthor(author,id) {
    var xhr = new XMLHttpRequest();
    var url="http://api.9smart.cn/apps?system=meego&author="+author
    xhr.open("GET",url,true);
    xhr.onreadystatechange = function()
    {
        if ( xhr.readyState == xhr.DONE)
        {
            if ( xhr.status == 200)
            {
                var jsonObject = eval('(' + xhr.responseText + ')');
                loadRelated(jsonObject,id)
            }
        }
    }

    xhr.send();
}

function loadRelated(jsonObject){
    if(parseInt(jsonObject.pager.count)  == 0 ){
        //moreAppsModel.clear()
    }
    else{
        for ( var i in jsonObject.apps   )
        {
            //console.log("HOW MUCH:"+jsonObject.apps[i].appname);
            if(id != jsonObject.apps[i].appid){
                moreAppsModel.append({
                                         "moreappid":jsonObject.apps[i].appid,
                                         "moreappname":jsonObject.apps[i].appname,
                                         "moreicon":jsonObject.apps[i].icon,
                                         "moreviews":jsonObject.apps[i].views,
                                         "moredownloads":jsonObject.apps[i].downloads
                                     }
                                     );
            }
        }

    }
}
function js_date_time(unixtime) {
    var timestr = new Date(parseInt(unixtime) * 1000);
    var datetime = timestr.toLocaleDateString();
    return datetime;
}
